<script setup>
import { ref, reactive, onMounted, computed } from 'vue';
import { ElMessage, ElTable, ElTableColumn, ElButton, ElInput, ElForm, ElFormItem, ElSelect, ElOption, ElPagination, ElDatePicker } from 'element-plus';

// 日志数据
const logData = ref([]);

// 搜索条件
const searchForm = reactive({
  operationUser: '',
  operationType: '',
  startTime: '',
  endTime: ''
});

// 分页相关
const currentPage = ref(1);
const pageSize = ref(10);

// 操作类型选项
const operationTypes = [
  { label: '登录', value: 'login' },
  { label: '登出', value: 'logout' },
  { label: '新增', value: 'add' },
  { label: '编辑', value: 'edit' },
  { label: '删除', value: 'delete' },
  { label: '查询', value: 'query' },
  { label: '导出', value: 'export' },
  { label: '导入', value: 'import' }
];

// 加载日志数据
const loadLogData = () => {
  // 模拟API请求，使用静态数据
  logData.value = [
    {
      id: 1,
      operationUser: 'admin',
      operationType: 'login',
      operationTime: '2025-10-16 10:00:15',
      operationIp: '127.0.0.1',
      operationModule: '系统管理',
      operationDesc: '用户登录成功'
    },
    {
      id: 2,
      operationUser: 'zhangsan',
      operationType: 'add',
      operationTime: '2025-10-16 09:30:45',
      operationIp: '192.168.1.101',
      operationModule: '线索管理',
      operationDesc: '新增线索信息'
    },
    {
      id: 3,
      operationUser: 'lisi',
      operationType: 'edit',
      operationTime: '2025-10-16 09:15:30',
      operationIp: '192.168.1.102',
      operationModule: '商机管理',
      operationDesc: '编辑商机信息'
    },
    {
      id: 4,
      operationUser: 'wangwu',
      operationType: 'delete',
      operationTime: '2025-10-16 08:45:20',
      operationIp: '192.168.1.103',
      operationModule: '客户管理',
      operationDesc: '删除客户信息'
    },
    {
      id: 5,
      operationUser: 'admin',
      operationType: 'query',
      operationTime: '2025-10-16 08:30:00',
      operationIp: '127.0.0.1',
      operationModule: '系统日志',
      operationDesc: '查询系统日志'
    }
  ];
};

// 计算总数
const total = computed(() => logData.value.length);

// 计算当前页显示的数据
const currentPageData = computed(() => {
  const start = (currentPage.value - 1) * pageSize.value;
  const end = start + Number(pageSize.value);
  return logData.value.slice(start, end);
});

// 搜索
const handleSearch = () => {
  currentPage.value = 1;
};

// 重置搜索条件
const handleReset = () => {
  Object.keys(searchForm).forEach(key => {
    searchForm[key] = '';
  });
  currentPage.value = 1;
};

// 处理分页
const handleCurrentChange = (val) => {
  currentPage.value = val;
};

// 处理每页条数变化
const handleSizeChange = (val) => {
  pageSize.value = val;
  currentPage.value = 1;
};

// 导出日志
const exportLog = () => {
  ElMessage.success('日志导出成功');
};

// 组件挂载时加载数据
onMounted(() => {
  loadLogData();
});
</script>

<template>
  <div class="system-log-container">
    <!-- 搜索区域 -->
    <div class="search-area">
      <el-form :model="searchForm" label-width="80px" inline>
        <el-form-item label="操作人">
          <el-input v-model="searchForm.operationUser" placeholder="请输入操作人" style="width: 150px;"></el-input>
        </el-form-item>
        <el-form-item label="操作类型">
          <el-select v-model="searchForm.operationType" placeholder="请选择操作类型" style="width: 150px;">
            <el-option
              v-for="type in operationTypes"
              :key="type.value"
              :label="type.label"
              :value="type.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间">
          <el-date-picker
            v-model="searchForm.startTime"
            type="datetime"
            placeholder="选择开始时间"
            style="width: 200px;"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间">
          <el-date-picker
            v-model="searchForm.endTime"
            type="datetime"
            placeholder="选择结束时间"
            style="width: 200px;"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="handleReset">重置</el-button>
          <el-button type="primary" @click="exportLog">导出日志</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 表格区域 -->
    <div class="table-area">
      <el-table :data="currentPageData" stripe style="width: 100%">
        <el-table-column prop="operationUser" label="操作人" width="120"></el-table-column>
        <el-table-column prop="operationType" label="操作类型" width="120">
          <template #default="{ row }">
            {{ operationTypes.find(type => type.value === row.operationType)?.label || row.operationType }}
          </template>
        </el-table-column>
        <el-table-column prop="operationTime" label="操作时间" width="180"></el-table-column>
        <el-table-column prop="operationIp" label="操作IP" width="150"></el-table-column>
        <el-table-column prop="operationModule" label="操作模块" width="150"></el-table-column>
        <el-table-column prop="operationDesc" label="操作描述" width="auto"></el-table-column>
      </el-table>
    </div>

    <!-- 分页区域 -->
    <div class="pagination-area">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[5, 10, 20, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      ></el-pagination>
    </div>
  </div>
</template>

<style scoped>
.system-log-container {
  padding: 20px;
}

.search-area {
  background-color: #f5f7fa;
  padding: 15px;
  border-radius: 4px;
  margin-bottom: 20px;
}

.table-area {
  background-color: #fff;
  border-radius: 4px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.pagination-area {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.el-button + .el-button {
  margin-left: 10px;
}
</style>